package com.qf.Servlet;

import com.qf.Service.PatientHistoryService;
import com.qf.Service.impl.PatientHistoryServiceImpl;
import com.qf.Service.PatientHistoryService;
import com.qf.Service.impl.PatientHistoryServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.ResultVo;
import com.qf.entity.PatientHistory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/patientHistoryServlet")
public class PatientHistoryServlet extends BaseServlet {
    PatientHistoryServiceImpl patientHistoryService = new PatientHistoryServiceImpl();
    //   查
    public void selrctByPatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String patientName = req.getParameter("patientName");
        PrintWriter pw = resp.getWriter();

        ResultVo<List<PatientHistory>> resultVo = new ResultVo<>();
        List<PatientHistory> patientHistories = patientHistoryService.selectByPatientName(patientName);
        if (patientHistories != null && patientHistories.size() > 0) {
            resultVo.setData(patientHistories);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }

        JsonUtils.showJson(pw, resultVo);
    }

    //增
    public void addPatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String patientIdcard = req.getParameter("patient_idcard");
        String patientName = req.getParameter("patient_name");
        String patientSym = req.getParameter("patient_sym");
        String patientDoctor = req.getParameter("patient_doctor");
        String patientMedicine = req.getParameter("patient_medicine");
        String patientHistoryDate = req.getParameter("patient_history_date");
        String takeprice = req.getParameter("takeprice");
        PatientHistory patientHistory = new PatientHistory();
        patientHistory.setPatient_Name(patientName);
        patientHistory.setPatient_Doctor(patientDoctor);
        patientHistory.setPatient_History_Date(patientHistoryDate);
        patientHistory.setPatient_Idcard(patientIdcard);
        patientHistory.setPatient_Medicine(patientMedicine);
        patientHistory.setId(Integer.parseInt(id));
        patientHistory.setPatient_Sym(patientSym);
        patientHistory.setTakeprice(Long.parseLong(takeprice));
        int num = patientHistoryService.addPatientName(patientHistory);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();

        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    改
    public void updatePatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String patientIdcard = req.getParameter("patient_idcard");
        String patientName = req.getParameter("patient_name");
        String patientSym = req.getParameter("patient_sym");
        String patientDoctor = req.getParameter("patient_doctor");
        String patientMedicine = req.getParameter("patient_medicine");
        String patientHistoryDate = req.getParameter("patient_history_date");
        String takeprice = req.getParameter("takeprice");
        PatientHistory patientHistory = new PatientHistory();
        patientHistory.setPatient_Name(patientName);
        patientHistory.setPatient_Doctor(patientDoctor);
        patientHistory.setPatient_History_Date(patientHistoryDate);
        patientHistory.setPatient_Idcard(patientIdcard);
        patientHistory.setPatient_Medicine(patientMedicine);
        patientHistory.setId(Integer.parseInt(id));
        patientHistory.setPatient_Sym(patientSym);
        patientHistory.setTakeprice(Long.parseLong(takeprice));
        int num = patientHistoryService.updatePatientName(patientHistory);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    删
    public void deleteByPatientName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String patientName, String patient_Idcard
        String patientName = req.getParameter("patientName");
        String patientIdcard = req.getParameter("patient_Idcard");
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        int num = patientHistoryService.deletePatientName(patientName, patientIdcard);
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }
}
